<?php

namespace app\admin\dao;

use app\common\model\LawCaseCategory;
use ShidianUtil\Thinkphp\Dao\DaoTrait;
use think\Collection;

/**
 * LawCaseCategoryDao 数据访问对象
 * Class LawCaseCategoryDao
 * @package app\admin\dao
 * @method LawCaseCategory findByPK(?int $id, $field = true)
 * @method \think\Collection selectAll(?int $page, ?int $page_size, $field = true)
 * @method LawCaseCategory insertActive(array $params, array $allow_field = [])
 * @method LawCaseCategory updateActive(LawCaseCategory $model, array $params, array $allow_field = [])
 * @method LawCaseCategory findAndUpdateActive(int $id, array $params, array $allow_field = [])
 * @method LawCaseCategory updateActiveByClosure(\Closure $closure, array $params, array $allow_field = [])
 * @method void deleteModel(LawCaseCategory $model)
 * @method void deleteById(int|array|\Closure $id, bool $force = false)
 * @link https://www.kancloud.cn/manual/thinkphp6_0/1037579
 * @link https://www.kancloud.cn/manual/think-orm/1257998
 * @link https://www.kancloud.cn/thinkphp/master-database-and-model/265551
 */
class LawCaseCategoryDao
{
    use DaoTrait;

    /**
     * @var LawCaseCategory
     */
    private $baseModel;

    /**
     * LawCaseCategoryDao constructor.
     */
    public function __construct()
    {
        $this->baseModel = LawCaseCategory::class;
    }

    /**
     * 可选的案件类型列表
     * @return Collection
     */
    public function selectOptions(): Collection
    {
        return LawCaseCategory::field(['id', 'name'])
            ->order('ord', 'ASC')
            ->order('id', 'desc')
            ->select();
    }

    public function listCategories(?int $page, ?int $page_size): array
    {
        $query = LawCaseCategory::withCount(['professionalQas'])
            ->field(true)
            ->append(['smallPicImg', 'smallPicImgMin'])
            ->order('ord', 'ASC')
            ->order('id', 'desc')
            ->paging($page, $page_size);

        return listOutput($query, $page_size);
    }

    /**
     * 法塘分类详情
     * @param $id
     * @return LawCaseCategory|null
     */
    public function findCategory($id): ?LawCaseCategory
    {
        return LawCaseCategory::withCount(['professionalQas'])
            ->where('id', $id)
            ->append(['smallPicImg'])
            ->find();
    }
}
